﻿--[[
   @brief    game utility functions
   @author   xiaokunyang(杨晓坤)
   @version  1.0
   @date     2020-09
--]]

game_utility =
{
    dynamic_states = {};
    notinstalled_states = {};
    init = function()
        game_utility.init_notinstalled_static();
        game_utility.init_dynamic_static();
    end;

    init_notinstalled_static = function()
        game_utility.notinstalled_states[ierd_tgp.game_library.GAME_STATE_UNKNOWN] = true;
        game_utility.notinstalled_states[ierd_tgp.game_library.GAME_STATE_NOT_INSTALLED] = true;
        game_utility.notinstalled_states[ierd_tgp.game_library.GAME_STATE_DOWNLOADING] = true;
        game_utility.notinstalled_states[ierd_tgp.game_library.GAME_STATE_DOWNLOAD_PAUSED] = true;
        game_utility.notinstalled_states[ierd_tgp.game_library.GAME_STATE_DOWNLOADED] = true;
        game_utility.notinstalled_states[ierd_tgp.game_library.GAME_STATE_DOWNLOAD_FAIL] = true;
        game_utility.notinstalled_states[ierd_tgp.game_library.GAME_STATE_INSTALLING] = true;
    end;

    init_dynamic_static = function()
        game_utility.dynamic_states[ierd_tgp.game_library.GAME_STATE_INSTALLING] = true;
        game_utility.dynamic_states[ierd_tgp.game_library.GAME_STATE_UPDATING] = true;
        game_utility.dynamic_states[ierd_tgp.game_library.GAME_STATE_REPAIRING] = true;
        game_utility.dynamic_states[ierd_tgp.game_library.GAME_STATE_LAUNCHING] = true;
        game_utility.dynamic_states[ierd_tgp.game_library.GAME_STATE_RUNNING] = true;
        game_utility.dynamic_states[ierd_tgp.game_library.GAME_STATE_UNINSTALLING] = true;
    end;

    get_attach_rel = function(game_id)
        local attach_info = comm_center.call_service("Svr_CheckHasAttachRel",
                {game_id = tonumber(game_id),
                 has_attach = 0,
                 select_id = 0,
                 main_game_id = 0,
                 sub_game_id = 0});

        return attach_info;
    end;

    has_attach_rel = function(game_id)
        local attach_info = game_utility.get_attach_rel_if_exist(game_id);
        if attach_info ~= nil then
            return true;
        end

        return false
    end;

    -- 是否附属游戏
    is_sub_in_attach_rel = function(game_id)
        local attach_info = game_utility.get_attach_rel_if_exist(game_id);
        if attach_info ~= nil and
                attach_info.sub_game_id == game_id then
            return true;
        end

        return false
    end;

    -- 返回合法的附属游戏信息，没有则返回nil
    get_attach_rel_if_exist = function(game_id)
        local attach_info = game_utility.get_attach_rel(game_id);
        if attach_info ~= nil and
                attach_info.has_attach == 1 and
                attach_info.sub_game_id ~= nil and
                attach_info.sub_game_id ~= 0 and
                attach_info.main_game_id ~= nil and
                attach_info.main_game_id ~= 0 then
            return attach_info;
        end

        return nil;
    end;

    -- 可以显示附属游戏入口（有权限）
    canshow_sub_entry = function(game_id)
        local canshow_info = comm_center.call_service("Svr_CheckGameCanShowEntry",
                {game_id = tonumber(game_id);
                 show_entry = 0;
                });
        if canshow_info.show_entry == 1 then
            return true;
        end

        return false;
    end;


    -- 获取游戏信息，调用者负责删除
    get_base_info = function(game_id)
        if game_id == nil or game_id == 0 then
            base_utility.log("[game_utility][get_base_info]game id invalid " ..
                    tostring(game_id),
                    true);
            return nil;
        end;

        local game_lib =
        base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if nil == game_lib then
            banner_control.inner_log("[game_utility][get_base_info]game_lib is nil",
                    true);
            return nil;
        end;

        local base_info = ierd_tgp.game_library.GameBaseInfo:new();
        if not game_lib:GetGameBaseInfo(game_id, base_info) then
            base_info:delete();
            banner_control.inner_log("[game_utility][get_base_info]GetGameBaseInfo failed",
                    true);
            return nil;
        end

        return base_info;
    end;

    is_in_noinstalled_state = function(istate)
        if game_utility.notinstalled_states[istate] == true then
            return true;
        else
            return false;
        end;
    end;

    is_in_dynamic_state = function(istate)
        if game_utility.dynamic_states[istate] == true then
            return true;
        else
            return false;
        end;
    end;

    can_uninstall_by_state = function(istate)
        return game_utility.can_modify_by_state(istate);
    end;

    can_modify_by_state = function(istate)
        if game_utility.is_in_noinstalled_state(istate) then
            return false;
        end;

        if game_utility.is_in_dynamic_state(istate) then
            return false;
        end;

        return true;
    end;

    is_game_support_fix = function(game_id)
        if nil == game_id then
            base_utility.log("[game_utility][is_game_support_fix]game_id nil",
                    true);
            return false;
        end

        if game_id == GAME_ID.GAME_ID_LOL or
                game_id == GAME_ID.GAME_ID_CF or
                game_id == GAME_ID.GAME_ID_DNF then
            return true;
        end

        local game_lib = base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if game_lib == nil then
            base_utility.log("[game_utility][is_game_support_fix]game_lib nil",
                    true);
            return false;
        end;

        local found = false;
        local result = false;

        local game_detail_info = ierd_tgp.game_library.GameDetailInfo:new_local();
        game_lib:get_game_detail_info(game_id, game_detail_info);

        if game_id == game_detail_info.game_id then
            base_utility.log("[game_utility][is_game_support_fix]got support_fix:" ..
                    tostring(game_detail_info.support_fix) ..
                    ", game_id:"  .. tostring(game_id));
            found = true;
            result = game_detail_info.support_fix;
        end

        if not found then
            base_utility.log("[game_utility][is_game_support_fix]game_detail_info NOT FOUND:" ..
                    tostring(game_id),
                    true);
            return false;
        end;

        return result;
    end;

    is_game_close_repair = function (game_id)
        local data = {key = "game_repair_switch"};
        local cfg = comm_center.call_service("i_svr_get_common_cfg", data);
        if nil == cfg then
            return false;
        end

        if cfg.value == nil or cfg.value == "" then
            return false;
        end

        local cfg_tb = base_utility.json_to_table(cfg.value);
        local is_close = false;
        for k, v in pairs(cfg_tb) do
            if v.game_id == "0" then
                is_close = true;
                break;
            elseif v.game_id == tostring(game_id) then
                is_close = true;
            end
        end
        return is_close;
    end;

    can_do_fix = function(game_id)
        if not game_utility.is_game_support_fix(game_id) then
            base_utility.log("[can_do_fix]fix NOT support:" .. tostring(game_id), true);
            return false;
        end
        local is_close = game_utility.is_game_close_repair(game_id);
        if true == is_close then
            base_utility.log("[can_do_fix]repaire closed:" .. tostring(game_id), true);
            return false;
        else
            local game_state = banner_model.get_game_state(game_id);
            if nil == game_state then
                base_utility.log("[can_do_fix]state is nil:" .. tostring(game_id), true);
                return false;
            end
            if not game_utility.can_modify_by_state(game_state.iState) then
                base_utility.log("[can_do_fix]" .. tostring(game_id) ..
                    " can not be modified in state:" .. tostring(game_state.iState), true);
                return false;
            end

            if game_state.extension_game_state.has_extensions then
                local extension_state = game_state.extension_game_state.iState;
                if not game_utility.can_modify_by_state(extension_state) then
                    base_utility.log("[can_do_fix]" .. tostring(game_id) ..
                            " extension can not be modified in state:" ..
                            tostring(game_state.extension_game_state.iState), true);
                    return false;
                end
            end

            -- 特殊逻辑, 后续去掉: dnf助手在微信登录的时候屏蔽
            if game_id == GAME_ID.GAME_ID_DNF then
                if TLoginModule.GetLoginAccountType() ~= TLoginModule.LOGIN_ACCOUNT_TYPE.kQQLogin then
                    base_utility.log("[can_do_fix]dnf can no be fixed in wechart login" ..
                            tostring(game_id));
                    return false;
                end
            end
            -- 特殊逻辑, 后续去掉
        end

        return true;
    end;

    can_do_uninstall = function(game_id)
        local state = banner_model.get_game_state(game_id);
        if state == nil or state.iState == nil then
            base_utility.log("[game_untility][can_do_uninstall]state is nil:" ..
                    tostring(state),
                    true);
            return false;
        end;

        if not game_utility.can_uninstall_by_state( state.iState) then
            base_utility.log("[game_untility][can_do_uninstall]can not uninstall" ..
                    ", game id:" .. tostring(game_id) ..
                    ", state:" .. tostring(state.iState), true);
            return false;
        end;

        if state.extension_game_state.has_extensions then
            local extension_state = state.extension_game_state.iState;
            if not game_utility.can_uninstall_by_state(extension_state) then
                base_utility.log("[game_untility][can_do_uninstall]" .. tostring(game_id) ..
                        " extension can not be modified in state:" ..
                        tostring(state.extension_game_state.iState), true);
                return false;
            end
        end

        base_utility.log("[game_untility][can_do_uninstall]true, state:" ..
                "game id:" .. tostring(game_id) ..
                "state:" .. tostring(state.iState));
        return true;
    end;
};

game_utility.init();